//Life Simulation Main C++ File
//Main Game Loop for Life Simulation
//Jeffrey Denton, Steven Flynn, Michael Weir	November 21, 2010

	//Project Include Files
#include "Meter.h"
#include "Calendar.h"
#include "Action.h"
	
	//Windows Include Files
#include <string>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>

	//Functions
void PrintName( HANDLE, char* );
void PrintDate( HANDLE, calendar ); 
void PrintMeters( HANDLE, meter, meter, meter, meter, meter );
void PrintButtons( HANDLE, int, char*[] );
void PrintActions(HANDLE, Action&, int);

void main()
{
		//DATA//
		//Basic Information
	char* name = "Bill Nye the Science Guy";	
	calendar date(2010, 11, 29, 1, 14);

		//Loop Variables
	bool isRunning = true;
	char input;
	
		//Action Variables
	Action actClass;
	action_mods actList;
	int action = 0;

		//Button Variables
	char* buttonList[4] = {"Main Menu", "Options", "Actions", "Store"};
	int button = 0;
		
		//Meter Variables
	meter happy		("Happiness",	50);
	meter energy	("Energy",		50);
	meter hunger    ("Hunger",		50);
	meter hygeine   ("Hygeine",		50);
	meter social  	("Social",		50);
	//meter meterArray[4] = {energy, hunger, hygeine, social};
	happy.SetStatus((energy.GetStatus() + hunger.GetStatus() + hygeine.GetStatus() + social.GetStatus()) / 4);

		//Text Modification Handle
	HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );

		//Validate hConsole
	if (INVALID_HANDLE_VALUE != hConsole)
	{	
			//Display Data//
		PrintName(hConsole, name);
		date.PrintDate(hConsole, 39, 0);
		PrintMeters(hConsole, happy, energy, hunger, hygeine, social);
		PrintActions(hConsole, actClass, action);
		PrintButtons(hConsole, button, buttonList);

		//Game Loop
		while (isRunning == true)		
		{
			if(_kbhit())				//Check for a keyboard hit
			{
				input = _getch();		//Read keyboard hit as input
				switch(input)
				{
				case 'q':				//Quit the game
					isRunning = false;
					break;
				case 'w':				//Move up in sub menu
				case 72:
					action--;
					if (action < 0)	//if end reached, stay put
					{
						action = 0;
					}
					break;
				case 's':				//Move down in sub menu
				case 80:
					action++;
					if (action >= actClass.getArraySize())	//if end reached, stay put
					{
						action = actClass.getArraySize() - 1;
					}
					break;
				case 'a':				//Move left in button bar
				case 75:
					button--;
					if (button < 0)		//if end reached, stay put
					{
						button = 0;
					}
					break;
				case 'd':				//Move right in button bar
				case 77:
					button++;
					if (button >= 4)	//if end reached, stay put
					{
						button = 3;
					}
					break;
				case 13:
						actList = actClass.getAct(action);
						energy.Update(actList.energy_mod);
						hunger.Update(actList.hunger_mod);
						hygeine.Update(actList.hygeine_mod);
						social.Update(actList.social_mod);
						happy.SetStatus((energy.GetStatus() + hunger.GetStatus() + hygeine.GetStatus() + social.GetStatus()) / 4);
						date.Update(actList.time_spent_mod);
						date.PrintDate(hConsole, 39, 0);
						PrintMeters(hConsole, happy, energy, hunger, hygeine, social);
						break;
						
				}
				PrintActions(hConsole, actClass, action);
				PrintButtons(hConsole, button, buttonList);

			}
		}
	}

	actClass.Release();
}

void PrintName(HANDLE hConsole, char* name)
{
	COORD pos = {0,0};
	SetConsoleCursorPosition(hConsole, pos);
	printf("%-40s", name);
}

void PrintMeters(HANDLE hConsole, meter hap, meter ene, meter hun, meter hyg, meter soc)
{
	meter meterList[5] = {hap, ene, hun, hyg, soc};
	COORD pos = {49,2};
	
	for(int i = 0; i < 5; i++, pos.Y++)
	{
		SetConsoleCursorPosition(hConsole, pos);
		meterList[i].Print();
	}
}

void PrintActions(HANDLE hConsole, Action& temp, int action)
{
	COORD pos = {0,2};
	SetConsoleCursorPosition(hConsole, pos);
	SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
	printf("Actions   Energy |Hunger |Hygeine|Social |Time   ");


	pos.Y += 1;
	for (int i = 0; i < temp.getArraySize(); i++, pos.Y++)
	{
		SetConsoleCursorPosition(hConsole, pos);
		if (i == action)
		{
			SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
			temp.Print(i);
		}
		else
		{
			SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
			temp.Print(i);
		}
		//printf("%d", i);
	}
}

void PrintButtons(HANDLE hConsole, int button, char* buttonList[])
{
	COORD pos = {0,19};
	
	
	for (int i = 0; i < 4; i++, pos.X += 20)
	{
		SetConsoleCursorPosition(hConsole, pos);
		if (i == button)
		{
			SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_GREEN);
			printf("%-s", buttonList[i]);
		}
		else
		{
			SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
			printf("%-s", buttonList[i]);
		}
	}

}